﻿//using MODI;
using Nop.Core;
using System;
using System.IO;
using System.Net;

namespace Nop.Services.WindAlert
{
    /// <summary>
    /// Winddata service
    /// </summary>
    public partial class WindHelpers : IWindHelpers
    {
        #region Fields
        private readonly IWebHelper _webHelper;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public WindHelpers(IWebHelper webHelper)
        {
            this._webHelper = webHelper;
        }
        #endregion

        #region wind data helpers

        public string GetPageDataFromImage(string imageUrl)
        {
            var fileDirectoryPath = _webHelper.MapPath("~/windalert/upload/");
            var filePath = Path.Combine(fileDirectoryPath, "image_text.gif");
            WebClient webClient = new WebClient();
            webClient.DownloadFile(imageUrl, filePath);
            return ExtractTextFromImage(filePath);
        }
        private string ExtractTextFromImage(string filePath)
        {
            //Document modiDocument = new Document();
            //modiDocument.Create(filePath);
            //modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH);
            //MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);
            //string extractedText = modiImage.Layout.Text;
            //modiDocument.Close();
            //return extractedText;
            return "";
        }

        public string GetPageData(string pageUrl)
        {
            bool IsHttp = (pageUrl.IndexOf("http:") != -1 || pageUrl.IndexOf("https:") != -1) ? true : false;
            if (String.IsNullOrEmpty(pageUrl))
                throw new NopException("windSationUrl null");
            string results = "Error";

            WebResponse objResponse;
            WebRequest objRequest = System.Net.HttpWebRequest.Create(pageUrl);

            if (IsHttp)
                objRequest.Method = "GET";
            else
                objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            try
            {
                objRequest.Timeout = 120000; // 20 sec.
                objResponse = objRequest.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream(), System.Text.Encoding.UTF7);
                results = sr.ReadToEnd();
                // Close and clean up the StreamReader
                sr.Close();
            }
            catch (Exception ex)
            {
                var t = ex.Message;
                results = null;
            }
            return results;
        }


        public string getDirectionNumToLetters(int intDir)
        {
            if (intDir > 348 && intDir < 360)
                return "N";
            else if (intDir > 11.25 && intDir < 33.75)
                return "NNE";
            else if (intDir > 33.75 && intDir < 56.25)
                return "NE";
            else if (intDir > 56.25 && intDir < 78.75)
                return "ENE";
            else if (intDir > 78.75 && intDir < 101.25)
                return "E";
            else if (intDir > 101.25 && intDir < 123.75)
                return "ESE";
            else if (intDir > 123.75 && intDir < 146.25)
                return "SE";
            else if (intDir > 146.25 && intDir < 168.75)
                return "SSE";
            else if (intDir > 168.75 && intDir < 191.25)
                return "S";
            else if (intDir > 191.25 && intDir < 213.75)
                return "SSW";
            else if (intDir > 213.75 && intDir < 236.25)
                return "SW";
            else if (intDir > 236.25 && intDir < 258.75)
                return "WSW";
            else if (intDir > 258.75 && intDir < 281.25)
                return "W";
            else if (intDir > 281.25 && intDir < 303.75)
                return "WNW";
            else if (intDir > 303.75 && intDir < 326.25)
                return "NW";
            else if (intDir > 326.25 && intDir < 348.75)
                return "NNW";
            else
                return "N";
        }

        public int getDirectionLettersToNum(string strDir)
        {
            switch (strDir.ToUpper())
            {
                case "N":
                    return Rand(348, 360);
                case "NNE":
                    return Rand(11.25, 33.75);
                case "NE":
                    return Rand(33.75, 56.25);
                case "ENE":
                    return Rand(56.25, 78.75);
                case "E":
                    return Rand(78.75, 101.25);
                case "ESE":
                    return Rand(101.25, 123.75);
                case "SE":
                    return Rand(123.75, 146.25);
                case "SSE":
                    return Rand(146.25, 168.75);
                case "S":
                    return Rand(168.75, 191.25);
                case "SSW":
                    return Rand(191.25, 213.75);
                case "SW":
                    return Rand(213.75, 236.25);
                case "WSW":
                    return Rand(236.25, 258.75);
                case "W":
                    return Rand(258.75, 281.25);
                case "WNW":
                    return Rand(281.25, 303.75);
                case "NW":
                    return Rand(303.75, 326.25);
                case "NNW":
                    return Rand(326.25, 348.75);
                default:
                    return 0;
            }
        }

        public double GetRandomPresentage(double Low, double High)
        {
            var rand = Rand(Low, High);
            double t =  rand / 100.0;
            return t;
        }

        private int Rand(double Low, double High)
        {
            Random rnd;
            rnd = new Random();
            return Convert.ToInt16(((High - Low + 1) * rnd.NextDouble()) + Low);
        }
        #endregion

    }
}
